/* 直线段 */
.text 
.data
.comm coordx,800,32
.comm coordy,800,32
.comm count,4,32

.section .rodata

.LC0:
	.string "expose-event"

.LC1:
	.long 0x0
	.long 0x3fe00000 /* 0.5 */

.LC2:
	.string "destroy"

.LC3:
	.string "button-press-event"

.LC4:
	.string "lines"


.text
.globl main

on_expose_event:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movq %rdi,-0x1000(%rbp) /* widget */
	movq %rsi,-0xff8(%rbp) /* event */
	movq %rdx,-0xff0(%rbp) /* data */

	movq $0,-0xfe8(%rbp) /* cr */
	movl $0,-0xfe0(%rbp) /* i */
	movl $0,-0xfdc(%rbp) /* j */

	movq -0x1000(%rbp),%rdi /* widget */
	movq 80(%rdi),%rdi /* widget->window */

	
	movl $0,%eax
	call gdk_cairo_create
	movq %rax,-0xfe8(%rbp) /* cr */

	pxor %xmm2,%xmm2
	pxor %xmm1,%xmm1
	pxor %xmm0,%xmm0 
	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax 
	call cairo_set_source_rgb 

	movsd .LC1(%rip),%xmm0 /* 0.5 */
	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax 
	call cairo_set_line_width 

	movl $0,-0xfe0(%rbp) /* i */
	jmp .Lon_expose_event_400

.Lon_expose_event_500:
	movl $0,-0xfdc(%rbp) /* j */
	jmp .Lon_expose_event_410

.Lon_expose_event_490:
	movl -0xfe0(%rbp),%eax /* i */
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordy(%rip),%rdi 
	movsd 0(%rdi,%rax,1),%xmm1 /* coordy[i] */
	movl -0xfe0(%rbp),%eax /* i */
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordx(%rip),%rdi 
	movsd 0(%rdi,%rax,1),%xmm0 /* coordx[i] */
	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax 
	call cairo_move_to 

	movl -0xfdc(%rbp),%eax /* j */
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordy(%rip),%rdi 
	movsd 0(%rdi,%rax,1),%xmm1 /* coordy[j] */
	movl -0xfdc(%rbp),%eax /* j */
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordx(%rip),%rdi 
	movsd 0(%rdi,%rax,1),%xmm0 /* coordx[j] */
	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax 
	call cairo_line_to 

	addl $1,-0xfdc(%rbp) /* j */

.Lon_expose_event_410:
	movl count(%rip),%eax
	subl $1,%eax
	cmpl -0xfdc(%rbp),%eax /* j */
	jge .Lon_expose_event_490

	addl $1,-0xfe0(%rbp) /* i */

.Lon_expose_event_400:
	movl -0xfe0(%rbp),%eax /* i */
	movl count(%rip),%ebx /* count */
	subl $1,%ebx
	cmpl %eax,%ebx
	jge .Lon_expose_event_500

	
	movl $0,count(%rip)
	
	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax
	call cairo_stroke 

	movq -0xfe8(%rbp),%rdi /* cr */
	movl $0,%eax
	call cairo_destroy 

	movl $0,%eax
	leave
	ret

clicked:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movq %rdi,-0x1000(%rbp) /* widget */
	movq %rsi,-0xff8(%rbp) /* event */
	movq %rdx,-0xff0(%rbp) /* user_data */

	movq -0xff8(%rbp),%rdi /* event */
	movl 52(%rdi),%eax /* event->button */

	cmpl $1,%eax
	jne .Lclicked_400

	movq -0xff8(%rbp),%rdi /* event */
	movsd 24(%rdi),%xmm0 /* event->x */

	movl count(%rip),%eax
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordx(%rip),%rdi 
	movsd %xmm0,0(%rdi,%rax,1) /* coordx[count] */

	movq -0xff8(%rbp),%rdi /* event */
	movsd 32(%rdi),%xmm0 /* event->y */

	movl count(%rip),%eax
	sall $3,%eax
	movslq %eax,%rax 
	leaq coordy(%rip),%rdi 
	movsd %xmm0,0(%rdi,%rax,1) /* coordy[count] */

	addl $1,count(%rip)

.Lclicked_400:
	movq -0xff8(%rbp),%rdi /* event */
	movl 52(%rdi),%eax /* event->button */
	cmpl $3,%eax
	jne .Lclicked_200

	movq -0x1000(%rbp),%rdi /* widget */
	movl $0,%eax
	call gtk_widget_queue_draw

.Lclicked_200:
	movl $1,%eax
	leave
	ret

main:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movl %edi,-0x1000(%rbp) /* argc */
	movq %rsi,-0xff8(%rbp) /* argv */

	movq $0,-0xff0(%rbp) /* window */

	leaq -0xff8(%rbp),%rsi /* argv */
	leaq -0x1000(%rbp),%rdi /* argc */
	movl $0,%eax
	call gtk_init 

	movl $0,%edi
	movl $0,%eax 
	call gtk_window_new 
	movq %rax,-0xff0(%rbp) /* window */

	movl $256,%esi
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call gtk_widget_add_events

	movl $0,%r9d
	movl $0,%r8d
	movl $0,%ecx
	leaq on_expose_event(%rip),%rdx 
	leaq .LC0(%rip),%rsi 
	movq -0xff0(%rbp),%rdi  /* window */
	movl $0,%eax
	call g_signal_connect_data

	movl $0,%r9d
	movl $0,%r8d
	movl $0,%ecx
	movq gtk_main_quit@GOTPCREL(%rip),%rdx 
	leaq .LC2(%rip),%rsi 
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call g_signal_connect_data

	movl $0,%r9d
	movl $0,%r8d
	movl $0,%ecx
	leaq clicked(%rip),%rdx 
	leaq .LC3(%rip),%rsi 
	movq -0xff0(%rbp),%rdi  /* window */
	movl $0,%eax
	call g_signal_connect_data

	movl $0,%eax 
	call gtk_window_get_type 
	movq %rax,%rsi 
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call g_type_check_instance_cast
	movl $1,%esi
	movq %rax,%rdi
	movl $0,%eax
	call gtk_window_set_position

	movl $0,%eax 
	call gtk_window_get_type 
	movq %rax,%rsi 
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call g_type_check_instance_cast

	leaq .LC4(%rip),%rsi
	movq %rax,%rdi
	movl $0,%eax
	call gtk_window_set_title


	movl $0,%eax 
	call gtk_window_get_type 
	movq %rax,%rsi 
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call g_type_check_instance_cast

	movl $300,%edx
	movl $400,%esi 
	movq %rax,%rdi
	movl $0,%eax 
	call gtk_window_set_default_size

	movl $1,%esi
	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call gtk_widget_set_app_paintable

	movq -0xff0(%rbp),%rdi /* window */
	movl $0,%eax
	call gtk_widget_show_all

	movl $0,%eax
	call gtk_main

	movl $0,%eax
	leave
	ret
